if (location.href.split("?")[1] == "bookcase") {
    $("label>input").attr("placeholder", "在我的书架中搜索")
} else {
    $("label>input").attr("placeholder", "请输入关键字")
}

//搜索历史记录
if (localStorage.searchHistory && localStorage.searchHistory.split(",").length >= 1) {
    history1()
}

function history1() {
    $(".p>span").remove();
    if (localStorage.searchHistory && localStorage.searchHistory.length > 0) {
        if (localStorage.searchHistory.indexOf(",") > 0) {
            var searchHistory = localStorage.searchHistory.split(",");
            if (searchHistory.length >= 20) {
                for (var h = 19; h >= 0; h--) {
                    var searchText = "<span>" + searchHistory[h] + "</span>"
                    $(".p").append(searchText);
                }
            } else {
                for (var h = (searchHistory.length - 1); h >= 0; h--) {
                    var searchText = "<span>" + searchHistory[h] + "</span>"
                    $(".p").append(searchText);
                }
            }
        } else {
            var searchText = "<span>" + localStorage.searchHistory + "</span>"
            $(".p").append(searchText);
        }
    }
}

// 点击取消返回上一页面
$(".close").on("click", function () {
    var act = $(this).attr('data-act');
    if (act != '') {
        location.href = "../novel/" + act;
    } else {
        window.history.back()
    }
})
//旋转45deg
$("label>a>span").css("transform", "rotate(45deg)")
$("input").on("focus", function () {
    if ($(this).val() != "") {
        $(".p").css("display", "none");
        if ($(".ul").css("display") == "block") {
            $(".homepage_list").css("display", "none")
        }
        $("label>a>span").css("display", "block")
    } else {
        if ($("nav").css("display") != "block") {
            $(".p").css("display", "block");
        } else {
            $(".p").css("display", "none");
        }
        $(".ul").css("display", "none");
        $("label>a>span").css("display", "none")
    }
}).on("input propertychange", function () {
    if ($(this).val() != "") {
        $(".p").css("display", "none");
        $(".ul").css("display", "block");
        $("nav").css("display", "none");
        $(".homepage_list").css("display", "none")
        $("label>a>span").css("display", "block")
        search_kw('pop');
    } else {
        history1()
        $(".p").css("display", "block");
        $(".ul").css("display", "none");
        $("label>a>span").css("display", "none");
        $("nav").css("display", "none");
        $(".homepage_list").css("display", "none")
    }
}).on("blur", function () {
    if ($(this).val()) {
        $(".p").css("display", "none");
        if ($(".ul").css("display") == "block") {
            $(".homepage_list").css("display", "none")
        }
        $("label>a>span").css("display", "block")
    } else {
        $(".p").css("display", "block");
        $(".ul").css("display", "none");
        $("label>a>span").css("display", "none");
    }

});

//软键盘回车搜索
var oForm = document.forms[0];
oForm.onsubmit = function () {
    if (trim($("#search").val()) == '') {
        return false;
    }
//    $("input").trigger("blur");
    $("nav").css("display", "block")
    $(".ul").css("display", "none")
    $(".p").css("display", "none")
    console.log('submit')
    search($("#search").val());
    var index = 0;
    for (var i = 0; i < $("nav>li").length; i++) {
        if ($("nav>li").eq(i).hasClass('active')) {
            index = i;
        }
    }
//        $(".homepage_list").eq(index).find("li").remove();
    search_kw('enter', index, '1');
};

$("input").on("keydown", function (e) {
    var key = e.which;
    if (key == 13) {
        e.preventDefault();
        console.log('enter')
        if (trim($("#search").val()) == '') {
            return false;
        }
//        $("input").trigger("blur");
        $("nav").css("display", "block")
        $(".ul").css("display", "none")
//            $(".homepage_list").eq(0).css("display", "block")
        $(".p").css("display", "none")
        search($("#search").val());
        var index = 0;
        for (var i = 0; i < $("nav>li").length; i++) {
            if ($("nav>li").eq(i).hasClass('active')) {
                index = i;
            }
        }
//            $(".homepage_list").eq(index).find('li').remove();
        search_kw('enter', index, '1');
    }
})

//清除输入框内容
$("label>a>span").on("click", function () {
    if ($("input").val()) {
        $("input").val("");
    }
    $(this).css("display", "none");
    $(".p").css("display", "block");
    $(".ul").css("display", "none");
    $("nav").css("display", "none");
    $(".content").css("display", "none");
    $(".homepage_list").css("display", "none")
});
//搜索热词
/**去掉字符串前后所有空格*/
function trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}
$(".p>span").on("click", function () {
    $("input").val(trim($(this).text()));
    $("label>a>span").css("display", "block");
    $(".p").css("display", "none");
    $(".ul").css("display", "block");
})
//清除搜索历史标签
$(".p>a").on("click", function () {
    $(".p>span").remove()
    localStorage.searchHistory = "";
})
$("nav>li").on("click", function () {
    $(window).scrollTop(0);
    $("nav>li").removeClass("active").attr("check", "false");
    $(".homepage_list").css("display", "none");
    $(this).addClass("active").attr("check", "true");
    $("nav>li").find("a").css("display", "none");
    $(this).find("a").css("display", "block");
    $(".homepage_list").eq($(this).index()).css("display", "block")
    var index = $(this).index();
//    for (var i = 0; i < $("nav>li").length; i++) {
//        if ($("nav>li").eq(i).hasClass('active')) {
//            index = i;
//        }
//    }
    var cached = $(".homepage_list").eq(index).attr('data-cached');
    if (cached != 'true') {
        $(".homepage_list").eq(index).find("li").remove();
        $(this).attr('data-page', 1);
        search_kw('enter', index);
    }
})
$(".homepage_list>li").on("click", function () {
    $(".ul>li").trigger("click")
})

function search_kw(sign, orderBy, s) {
    $(".content").css("display", "none");
    var search = trim($("#search").val());
    if (search == '') {
        return false;
    }
    var page = $("nav>li").eq(orderBy).attr('data-page');
    var pageSize = 10;
    if (sign == 'pop') {
        orderBy = 999;
        page = 1;
        pageSize = 5;
    } else {
        if (s == '1') {
            page = 1;
            $(".homepage_list").eq(orderBy).find('li').remove();
            $(".homepage_list").eq(orderBy).css("display", "block")
        }
    }

    $.ajax({
        url: "/novel/novel/search",
        type: "post",
        dataType: "json",
        async: false,
        data: {kw: search, page: page, pageSize: pageSize, orderBy: orderBy},
        success: function (data) {
            if (data.status == 0) {
                var list = data.data.list.list;
                var str = '';
                if (sign == 'pop') {
                    for (i in list) {
                        str += '<li data-id="' + list[i]['novel_id'] + '">' +
                                '<img src="/public/img/images/search_icon_03.png">' +
                                list[i]['novel_title'] +
                                '</li>';
                    }
                    $(".ul").empty();
                    $(".ul").append(str);
                    //点击搜索结果保存搜索值
                    var searchCont = list[i]['novel_title'];
                    $(".ul>li").on("click", function () {
                        if (localStorage.searchHistory) {
                            if (localStorage.searchHistory.split(",").indexOf(searchCont) < 0) {
                                localStorage.searchHistory += "," + searchCont
                            } else {
                                localStorage.searchHistory = localStorage.searchHistory;
                            }
                        } else {
                            localStorage.searchHistory = searchCont;
                        }
                        var novelid = $(this).attr('data-id');
                        location.href = "../novel/novelInfo?novelid=" + novelid;
                        console.log(localStorage.searchHistory)
                    })
                } else {
                    if (list.length <= 0) {
                        $("nav").css("display", "none")
                        $(".content").css("display", "block");
                        return false;
                    }
                    for (i in list) {
                        str += '<li data-id="' + list[i]['novel_id'] + '">' +
                                '<img src="' + list[i]['novel_cover'] + '">' +
                                '<div>' +
                                '<h4>' + list[i]['novel_title'] + '</h4>' +
                                '<h6>' + list[i]['novel_introduction'] + '</h6>' +
                                '<p>' +
                                '<span>作者 ' + list[i]['novel_author'] + '</span>' +
                                '<a href="javascript:;">' + list[i]['novel_type'] + '</a>' +
                                '<a href="javascript:;">' + list[i]['novel_category'] + '</a>' +
                                '</p>' +
                                '</div>' +
                                '<a href="javascript:;"></a>' +
                                '</li>';
                    }
                    if (search != data.data.params.kw) {
                        $(".homepage_list").eq(orderBy).find('li').remove();
                    }
                    $(".homepage_list").eq(orderBy).append(str);
                    $(".homepage_list").eq(orderBy).attr('data-cached', true);
                    $("nav>li").eq(orderBy).attr('data-page', data.data.params.page);
                }

                //点击书籍进入对应的书籍详情
                $(".homepage_list>li").on("click", function () {
                    var nid = $(this).attr('data-id');
                    location.href = "../novel/novelInfo?novelid=" + nid;
                })
            }
        },
        error: function (e) {
            $(".message_info_alert>span").html("系统繁忙请稍后再试");
            $(".message_info_alert").fadeIn(function () {
                setTimeout(function () {
                    $(".message_info_alert").fadeOut()
                }, 300)
            })
        }
    });
}

$(window).on("scroll", function () {
    if ($(this).scrollTop() >= $(document).height() - $(window).height()) {
        // 加载剩余书籍
        var index = 0;
        for (var i = 0; i < $("nav>li").length; i++) {
            if ($("nav>li").eq(i).attr("check") == "true") {
                index = i;
            }
        }
        var kw = $("#search").val();
        var url = "novel/novel/search?kw=" + kw + "&orderBy=" + index;
        window.history.pushState({}, 0, 'http://' + window.location.host + '/' + url);
        search_kw('enter', index);
    }
})

//点击搜索结果保存搜索值
$(".ul>li").on("click", function () {
    if(trim($(this).text())){
        search($(this).text())
    }
    
})
$(".homepage_list>li").on("click", function () {
    if(trim($(this).find("h4").text())){
        search($(this).find("h4").text())
    }
})

function search(sCont) {
    var searchCont = sCont;
    if (localStorage.searchHistory) {
        if (localStorage.searchHistory.split(",").indexOf(searchCont) < 0) {
            localStorage.searchHistory += "," + searchCont
        } else {
            localStorage.searchHistory = localStorage.searchHistory;
        }
    } else {
        localStorage.searchHistory = searchCont;
    }
}





